<template>
  <div class="full">
    <el-scrollbar>
      <div class="dlText-120 list-b-12 p-12">
        <CellBox title="生产信息">
          <FlexBox :list="3">
            <ElFormItemBox label="生产方式" :prop="`productionMode`">
              <ElSelectBox :data="data.baseData" dataName="productionMode" :options="proxy.$optionsArr.productionMode"
                :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="生产尺寸" :prop="`productionDimensions`">
              <ElInputBox :data="data.baseData" dataName="productionDimensions"
                :unit="data.baseData.productionMode === 1 ? 'T' : 'mm'" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="印刷拼版" :prop="`makeupPrintingCombine`">
              <ElInputBox :data="data.baseData" dataName="makeupPrintingCombine" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="刀模模数" :prop="`makeupModules`">
              <ElInputBox :data="data.baseData" dataName="makeupModules" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="平张断张" :prop="`makeupPzDz`">
              <ElInputBox :data="data.baseData" dataName="makeupPzDz" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="色数" :prop="`makeupColors`">
              <ElInputBox :data="data.baseData" dataName="makeupColors" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="间距" :prop="`makeupSpaceBetween`">
              <ElInputBox :data="data.baseData" dataName="makeupSpaceBetween" unit="mm" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="跳距" :prop="`makeupSkipDistance`">
              <ElInputBox :data="data.baseData" dataName="makeupSkipDistance" unit="mm" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="留边尺寸" :prop="`marginSize`">
              <ElInputBox :data="data.baseData" dataName="marginSize" unit="mm" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="每组高度" :prop="`eachGroupHeight`">
              <ElNumberBox :data="data.baseData" dataName="eachGroupHeight" unit="mm" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="每组数量" :prop="`eachGroupQuantity`">
              <ElNumberBox :data="data.baseData" dataName="eachGroupQuantity" unit="mm" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="每刀高度" :prop="`knifeTimes`">
              <ElNumberBox :data="data.baseData" dataName="knifeTimes" unit="mm" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="刀模信息" :prop="`knifeInformation`" class="wAuto">
              <ElInputBox :data="data.baseData" dataName="knifeInformation" disabled />
              <template #btn v-if="isEdit">
                <el-button @click="() => {
                  data.baseData.knifeInformation = ''
                  data.baseData.knifeVersionCode = ''
                }" v-if="data.baseData.knifeVersionCode">清除</el-button>
                <SelectEditionAdminKnifeMold @selectBtn="(arr: any) => {
                  data.baseData.knifeInformation = `${arr[0].knifeVersionNo}${arr[0].knifeVersionName}`
                  data.baseData.knifeVersionCode = arr[0].knifeVersionCode
                }" />
              </template>
            </ElFormItemBox>
            <ElFormItemBox label="丝网版信息" :prop="`plateInformation`" class="wAuto">
              <ElInputBox :data="data.baseData" dataName="plateInformation" :disabled="!isEdit" />
            </ElFormItemBox>
            <ElFormItemBox label="生产备注" :prop="`productionRequirement`" class="wAuto">
              <ElInputBox :data="data.baseData" dataName="productionRequirement" type="textarea" :disabled="!isEdit" />
            </ElFormItemBox>
          </FlexBox>
        </CellBox>
        <el-divider />
        <CellBox title="工艺信息">
          <TableBoxMx :data="{
            disabled: !isEdit,
            tbody: data.productTechniqueList,
            thead: [
              {
                label: '工艺编号',
                prop: 'craftCode',
                align: 'center',
                width: 80,
              },
              {
                label: '工艺名称',
                prop: 'craftCode',
                align: 'left',
                width: 220,
                formType: 'select', //文件类型
                formProp: `productTechniqueList`,//必填
                options: options.produceCraft,
                changeBtn: (value: string, item: any, row: any) => {
                  proxy.$api.ProductionBusniess.SureChooseProduceCraftEquipmentMaps({
                    craftCodes: [value]
                  })
                    .then((res: any) => {
                      item.deviceCode = ''
                      item.produceMachineList = []
                      if (res.length > 0) {
                        const _craftEquipments = res[0].craftEquipments.map((el: any) => ({
                          machineName: el.equipmentName,
                          machineCode: el.equipmentCode
                        }))
                        item.produceMachineList = _craftEquipments
                        if (_craftEquipments.length === 0) {
                          item.craftCode = ''
                          proxy.$message('该工艺未关联设备，请先添加', 'error')
                        } else {
                          item.deviceCode = _craftEquipments[0].machineCode
                        }
                      }
                    })
                    .catch(() => { });
                }
              },
              {
                label: '使用设备',
                prop: 'deviceCode',
                align: 'left',
                width: 160,
                formType: 'select', //文件类型
                formProp: `productTechniqueList`,//必填
                optionsRow: 'produceMachineList',
                labelName: 'machineName',
                valueName: 'machineCode',
              },
              {
                label: '备注',
                prop: 'craftRequirement',
                align: 'left',
                formType: 'text', //文件类型
              }
            ],
            //排序key
            sortKey: 'produceCraftSort',
            //操作
            operation: [
              {
                label: '复制',
                icon: 'CopyDocument',
                notArr: ['productTechniqueID', 'productTechniqueCode']
              },
              {
                label: '删除',
                icon: 'Delete'
              }
            ],
            add: {
              produceMachineList: [],
              productTechniqueID: 0, // 主键
              productTechniqueCode: '', // 唯一编码
              productCode: '', // 产品编码
              craftCode: '', // 工艺编码
              deviceCode: '', // 设备编码
              craftRequirement: '', // 工艺要求
              productTechniqueCardState: 0 // 工艺资料卡状态：0未填写；1已填写（待锁定）；2已锁定
            }
          }">
          </TableBoxMx>
        </CellBox>
        <el-divider />
        <CellBox title="材料信息">
          <GoodList :data="{
            disabled: !isEdit,
            height: 290,
            rowKey: 'specification',
            prop: 'productMaterialList',
            tbody: data?.productMaterialList,
            thead: [
              {
                label: '使用类型',
                prop: 'useType',
                align: 'center',
                width: 120,
                formType: 'select',
                options: [
                  {
                    label: '常用料',
                    value: 0,
                  },
                  {
                    label: '备用料',
                    value: 1,
                  }
                ]
              },
              {
                label: '备注',
                prop: 'produceMaterialRemark',
                formType: 'textarea'
              },
            ],
            operation: [
              {
                label: '删除',
                icon: 'Delete',
              },
            ],
          }" @selectBtn="addMx">
          </GoodList>
        </CellBox>
        <el-divider />
        <el-affix position="bottom" :offset="60" v-if="isEdit">
          <div class="flex-center-center list-r-8">
            <SelectProduct @selectBtn="copyProductBtn">
              <el-button>复制产品</el-button>
            </SelectProduct>
            <el-button type="primary" @click="saveBtn">保存生产信息</el-button>
          </div>
        </el-affix>
      </div>
    </el-scrollbar>
  </div>
</template>
<script setup lang="ts">
import '../pageCss.scss'; // 引入样式
const { proxy } = getCurrentInstance() as any;
const props = withDefaults(
  defineProps<{
    data?: any;//数据源
    isEdit?: boolean;//是否編輯
    options?: any;//数据组
    setState?: Function;//更改状态
  }>(),
  {
  },
);
const emit = defineEmits<{
  //fn为父组件引入子组件是定义的子组件事件属性，可随意定义
  (event: 'saveBtn'): void;
}>();
//添加关联单明细
const addMx = (rows: any) => {
  const _rows = Array.isArray(rows) ? rows : [rows]
  let _arr = _rows.map((el: any) => ({
    specification: el.specificationCode, // 规格编号
    materialType: el.goodsGroupName, // 分组名称
    materialName: el.goodsName, // 物料名称
    stockGoodsName: el.specificationName, // 规格名称
    supplierName: el.supplierName, // 规格名称
    categoryName: el.goodsTypeName, // 物料类型名称
    useType: 0, // 使用类型
    materialRemark: '', // 备注
  }))
  props.data.productMaterialList.push(
    ..._arr
  )
}
//复制产品信息
const copyProductBtn = (arr: any) => {
  let _row = arr[0]
  let _data = props.data
  proxy.$api.OrderBusniess.GetSingleProductByProductCode({
    queryBy: 2,
    queryValue: _row.productCode
  })
    .then((res: any) => {
      _data.productMaterialList = res.productMaterialList.map((el: any) => ({
        ...el,
        productMaterialID: '',
        productMaterialCode: '',
      }))
      _data.productTechniqueList = res.productTechniqueList.map((el: any) => ({
        ...el,
        productTechniqueID: '',
        productTechniqueCode: '',
        productTechniqueCardState: 0,
      }))
      //生产信息
      let arrItem = [
        "productionMode", //生产方式
        "productionDimensions", //生产尺寸
        "makeupPrintingCombine", //印刷拼版
        "makeupModules", //刀模模数
        "makeupPzDz", //平张断张
        "makeupColors", //色数
        "makeupSpaceBetween", //间距
        "makeupSkipDistance", //跳距
        "marginSize", //留边尺寸
        "eachGroupHeight", //每组高度
        "eachGroupQuantity", //每组数量
        "knifeTimes", //每刀高度
        "knifeInformation", //刀版信息
        "knifeVersionCode",//刀版code
        "plateInformation", //印版信息
        "productionRequirement" //生产备注
      ];
      for (let i = 0; i < arrItem.length; i++) {
        _data.baseData[arrItem[i]] = res.baseData[arrItem[i]];
      }
    })
    .catch(() => { });
};
const saveBtn = () => {
  Promise.all([
    UpdateProductProductionData(),//修改产品生产信息
    UpdateProductTechnique(),//修改生产工艺信息
    UpdateProductMaterial(),//修改生产材料信息
  ]).then((res: any) => {
    if (props.setState && typeof props.setState === 'function') {
      props.setState(4, '保存成功')
    }
  }).catch(() => {

  });
};
//修改产品生产信息
const UpdateProductProductionData = () => {
  return new Promise((resolve, reject) => {
    let _data = props.data
    proxy.$api.OrderBusniess.UpdateProductProductionData(_data.baseData)
      .then((res: any) => {
        resolve(res)
      })
      .catch((e: any) => {
        reject(e)
      });
  });
};
//修改生产工艺信息
const UpdateProductTechnique = () => {
  return new Promise((resolve, reject) => {
    let _data = props.data
    let _productCode = _data.baseData.productCode
    proxy.$api.OrderBusniess.UpdateProductTechnique({
      productCode: _productCode, // 产品编码
      productTechniqueList: _data.productTechniqueList.map((el: any) => ({
        productTechniqueID: el.productTechniqueID, // 主键
        productTechniqueCode: el.productTechniqueCode, // 唯一编码
        productCode: _productCode, // 产品编码
        craftCode: el.craftCode, // 工艺编码
        deviceCode: el.deviceCode, // 设备编码
        craftRequirement: el.craftRequirement, // 工艺要求
        productTechniqueCardState: el.productTechniqueCardState || 0 // 工艺资料卡状态：0未填写；1已填写（待锁定）；2已锁定
      }))
    })
      .then((res: any) => {
        resolve(res)
      })
      .catch((e: any) => {
        reject(e)
      });
  });
};
//修改生产材料信息
const UpdateProductMaterial = () => {
  return new Promise((resolve, reject) => {
    let _data = props.data
    let _productCode = _data.baseData.productCode
    proxy.$api.OrderBusniess.UpdateProductMaterial({
      productCode: _productCode, // 产品编码
      productMaterialList: _data.productMaterialList.map((el: any) => ({
        productMaterialID: el.productMaterialID, // 主键
        productMaterialCode: el.productMaterialCode, // 唯一编码
        productCode: _productCode, // 产品编码
        stockGoodsCode: el.stockGoodsCode, // 物料编码,Goods.GoodsCode
        materialName: el.materialName, // 物料名称
        specification: el.specification, // 规格型号编码，GoodsSpecification.SpecificationCode
        materialType: el.materialType, // 物料类型
        useType: el.useType || 0, // 使用类型，0：常用料；1：备用料
        materialRemark: el.materialRemark // 物料备注
      }))
    })
      .then((res: any) => {
        resolve(res)
      })
      .catch((e: any) => {
        reject(e)
      });
  });
};
import { getProductType, getProduceCraft } from '@/utils/optionsArr.ts';
onMounted(() => {
  getProductType() //获取产品类型
  getProduceCraft(props.options, 'produceCraft') //获取工艺数据
});
</script>
